package com.rdgk.newtech.controller;

import com.alibaba.fastjson.JSONObject;
import com.rdgk.newtech.service.KeywordService;
import com.rdgk.newtech.util.CommonUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: msun
 * @description: 关键词相关Controller
 */
@RestController
@RequestMapping("/keyword")
public class KeywordController {

    @Autowired
    private KeywordService keywordService;

    /**
     * 显示关键词列表
     */
    @RequiresPermissions("article:list")
    @GetMapping("/listKeyword")
    public JSONObject listDepartment(HttpServletRequest request) {
        System.out.println(CommonUtil.request2Json(request));
        return keywordService.listKeyword(CommonUtil.request2Json(request));
    }

    /**
     * 搜索关键词列表
     */
    @RequiresPermissions("article:list")
    @GetMapping("/listKeywordBySearch")
    public JSONObject listDepartmentBySearch(HttpServletRequest request) {
        return keywordService.listKeywordBySearch(CommonUtil.request2Json(request));
    }

    /**
     * 查询关键词是否存在(按关键词名称进行查询)
     */
    @RequiresPermissions("article:list")
    @GetMapping("/getKeywordtByName")
    public JSONObject getKeywordtByName(HttpServletRequest request) {
        System.out.println(CommonUtil.request2Json(request));
        return keywordService.getKeywordCountByName(CommonUtil.request2Json(request));
    }

    /**
     * 新增关键词
     */
    @RequiresPermissions("article:add")
    @PostMapping("/addKeyword")
    public JSONObject addKeyword(@RequestBody JSONObject requestJson) {
//        CommonUtil.hasAllRequired(requestJson, "keywordName, keywordTopic，keywordTopicId, keywordMark");
        return keywordService.addKeyword(requestJson);
    }

    /**
     * 修改关键词
     */
    @RequiresPermissions("article:update")
    @PostMapping("/updateKeyword")
    public JSONObject updateKeyword(@RequestBody JSONObject requestJson) {
        System.out.println("requestJson:"+requestJson);
        CommonUtil.hasAllRequired(requestJson, "keywordId, keywordName, keywordTopic, keywordTopicId, keywordMark");
        return keywordService.updateKeyword(requestJson);
    }

    /**
     * 删除关键词
     */
    @RequiresPermissions("article:delete")
    @PostMapping("/deleteKeyword")
    public JSONObject deleteKeyword(@RequestBody JSONObject requestJson) {
        System.out.println("requestJson:"+requestJson);
        CommonUtil.hasAllRequired(requestJson, "id");
        return keywordService.deleteKeyword(requestJson);
    }

    /**
     * 获取主题词
     */
    @RequiresPermissions("article:delete")
    @GetMapping("/listTopic")
    public JSONObject listTopic(HttpServletRequest request) {
        return keywordService.listTopic(CommonUtil.request2Json(request));
    }

}
